package com.gitee.linmt.properties;

import com.gitee.linmt.util.MessageUtil;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;

/**
 * Result 配置.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@ConfigurationProperties(prefix = "application.result", ignoreInvalidFields = true)
public class ResultProperties {
    
    public static final String FILE_SUFFIX_YAML = ".yaml";
    
    public static final String FILE_SUFFIX_YML = ".yml";
    
    private final Logger logger = LoggerFactory.getLogger(ResultProperties.class);
    
    /**
     * 默认Message的文件名称.
     */
    private String messageFile = "message.yaml";
    
    /**
     * 国际化的Message配置文件所在的文件夹.
     */
    private String i18nFolder = "message" + File.separator;
    
    /**
     * 请求头中，Message国际化的参数的 key.
     */
    private String i18nHeaderKey = "Accept-Language";
    
    /**
     * 是否开启日志打印.
     */
    private Boolean enableLogger = Boolean.TRUE;
    
    /**
     * 日志格式.
     */
    private String loggerFormat = "请求地址：{} 请求方法：{} 响应数据：{}";
    
    public String getI18nFolder() {
        return this.i18nFolder;
    }
    
    /**
     * 设置Message配置文件所在的文件夹.
     *
     * @param i18nFolder 文件夹名称
     */
    public void setI18nFolder(String i18nFolder) {
        if (Strings.isBlank(i18nFolder)) {
            this.logger.info("未设置文件路径，使用默认值：{}", this.i18nFolder);
            i18nFolder = this.i18nFolder;
        } else {
            if (!i18nFolder.endsWith(File.separator)) {
                i18nFolder += File.separator;
            }
        }
        this.i18nFolder = i18nFolder;
    }
    
    public String getMessageFile() {
        return this.messageFile;
    }
    
    public void setMessageFile(String messageFile) {
        if (!messageFile.endsWith(ResultProperties.FILE_SUFFIX_YAML) && !messageFile
                .endsWith(ResultProperties.FILE_SUFFIX_YML)) {
            throw new RuntimeException(MessageUtil
                    .format("不支持该文件格式：{}，仅支持格式为 {} 和 {} 的文件", messageFile, ResultProperties.FILE_SUFFIX_YAML,
                            ResultProperties.FILE_SUFFIX_YML));
        }
        if (Strings.isBlank(messageFile)) {
            this.logger.info("未设置默认文件名，使用默认文件名：{}", this.messageFile);
            messageFile = this.messageFile;
        }
        this.messageFile = messageFile;
    }
    
    public String getI18nHeaderKey() {
        return this.i18nHeaderKey;
    }
    
    public void setI18nHeaderKey(String i18nHeaderKey) {
        if (Strings.isBlank(i18nHeaderKey)) {
            this.logger.info("未设置请求头的 Key，使用默认值：{}", this.i18nHeaderKey);
            i18nHeaderKey = this.i18nHeaderKey;
        }
        this.i18nHeaderKey = i18nHeaderKey.trim();
    }
    
    public Boolean getEnableLogger() {
        return this.enableLogger;
    }
    
    public void setEnableLogger(final Boolean enableLogger) {
        if (enableLogger == null) {
            this.enableLogger = Boolean.FALSE;
        }
        this.enableLogger = enableLogger;
    }
    
    public String getLoggerFormat() {
        return this.loggerFormat;
    }
    
    public void setLoggerFormat(String loggerFormat) {
        if (Strings.isBlank(loggerFormat)) {
            this.logger.info("未设置日志格式，使用默认值：{}", this.loggerFormat);
            loggerFormat = this.loggerFormat;
        }
        this.loggerFormat = loggerFormat;
    }
}
